package com.beiing.tianshuai.tianshuai.mine.ui;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.beiing.tianshuai.tianshuai.R;
import com.beiing.tianshuai.tianshuai.activity.CommonWebViewActivity;
import com.beiing.tianshuai.tianshuai.app.AppConstant;
import com.beiing.tianshuai.tianshuai.base.BaseActivity;
import com.beiing.tianshuai.tianshuai.nim.login.LogoutHelper;
import com.beiing.tianshuai.tianshuai.util.DataCleanManager;
import com.beiing.tianshuai.tianshuai.util.SPUtils;
import com.beiing.tianshuai.tianshuai.widget.CustomDialog;
import com.kyleduo.switchbutton.SwitchButton;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import cn.bingoogolapple.bgabanner.BGAOnNoDoubleClickListener;
import cn.jpush.android.api.JPushInterface;

/**
 * @author Administrator
 */
public class SettingsActivity extends BaseActivity {

    @BindView(R.id.toolbar_iv_back)
    ImageView mToolbarIvBack;
    @BindView(R.id.toolbar_tv_title)
    TextView mToolbarTvTitle;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.btn_message_inform)
    TextView mBtnMessageInform;
    @BindView(R.id.btn_about)
    TextView mBtnAbout;
    @BindView(R.id.btn_about_app)
    TextView mBtnAboutApp;
    @BindView(R.id.btn_clear_cache)
    RelativeLayout mBtnClearCache;
    @BindView(R.id.btn_logout)
    TextView mBtnLogout;
    @BindView(R.id.txt_cache_size)
    TextView mTvCacheSize;
    @BindView(R.id.sb_md)
    SwitchButton mSbMd;

    /**
     * 管理我的地址
     */
    @BindView(R.id.btn_address_manage)
    TextView mTvAddressManage;

    private Unbinder mUnbinder;
    private Context mContext;
    private CustomDialog mDialog;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_settings;
    }

    @Override
    protected void init() {
        mUnbinder = ButterKnife.bind(this);
        mContext = this;
        initToolbar();
        // 设置消息通知开关状态为当前用户设置的开关状态
        boolean isPushStopped = JPushInterface.isPushStopped(mContext.getApplicationContext());
        mSbMd.setChecked(!isPushStopped);
        try {
            mTvCacheSize.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
        } catch (Exception e) {
            e.printStackTrace();
        }
        initListener();
    }

    @Override
    protected void onResume() {
        if (isLogin()) {
            mBtnLogout.setText("退出登录");
        } else {
            mBtnLogout.setText("登录");
        }
        super.onResume();
    }

    private void initToolbar() {
        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        mToolbarIvBack.setVisibility(View.VISIBLE);
        mToolbarIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        mToolbarTvTitle.setText("设置");
    }

    private void initListener() {
        // 设置消息通知
        mBtnMessageInform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent localIntent = new Intent();
                localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                localIntent.setAction("android.settings.APPLICATION_DETAILS_SETTINGS");
                localIntent.setData(Uri.fromParts("package", getPackageName(), null));
                startActivity(localIntent);
            }
        });

        mSbMd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if (b) {
                    JPushInterface.resumePush(mContext.getApplicationContext());
                    SPUtils.put(mContext, "is_push_resume", true);
                } else {
                    JPushInterface.stopPush(mContext.getApplicationContext());
                    SPUtils.put(mContext, "is_push_resume", false);
                }
            }
        });
        //管理我的收货地址
        mTvAddressManage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(mContext,OrderAddressManageActivity.class);
                startActivity(intent);
            }
        });
        // 关于我们
        mBtnAbout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, CommonWebViewActivity.class);
                intent.putExtra("title", "关于我们");
                intent.putExtra("url", "http://www.tianshuai.com.cn/index.php/Home/Index/help");
                startActivity(intent);
            }
        });

        //关于天率校园
        mBtnAboutApp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, AboutAppActivity.class);
                startActivity(intent);
            }
        });

        // 清除缓存
        mBtnClearCache.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                CustomDialog dialog = new CustomDialog(mContext, R.style.dialog,
                        "清除缓存可能会导致额外的流量消耗，确定继续清除吗？",
                        new CustomDialog.OnCloseListener() {
                            @Override
                            public void onClick(Dialog dialog, boolean confirm) {
                                if (confirm) {
                                    clearCache();
                                    dialog.dismiss();
                                } else {
                                    dialog.dismiss();
                                }
                            }
                        }).setTitle("提示");
                dialog.setCanceledOnTouchOutside(true);
                dialog.show();
            }
        });

        // 退出登陆
        mBtnLogout.setOnClickListener(new BGAOnNoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View view) {
                if (isLogin()) {
                    mDialog = new CustomDialog(mContext, R.style.dialog,
                            "确定要退出登陆吗？",
                            new CustomDialog.OnCloseListener() {
                                @Override
                                public void onClick(Dialog dialog, boolean confirm) {
                                    if (confirm) {
                                        SPUtils.clear(mContext);
                                        LogoutHelper.logout();
                                        JPushInterface.deleteAlias(mContext, AppConstant.MSG_DEL_ALIAS);
                                        mDialog.dismiss();
                                        finish();
                                    } else {
                                        dialog.dismiss();
                                    }
                                }
                            }).setTitle("提示");
                    mDialog.setCanceledOnTouchOutside(true);
                    mDialog.show();
                } else {
                    Intent intent = new Intent(mContext, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private void clearCache() {
        DataCleanManager.clearAllCache(getApplicationContext());
        try {
            mTvCacheSize.setText(DataCleanManager.getTotalCacheSize(getApplicationContext()));
            showShortText("清除成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isLogin() {
        String uid = SPUtils.getString(mContext, "uid", null);
        return !TextUtils.isEmpty(uid);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mUnbinder.unbind();
    }
}